package com.anan.service.impl;

import com.anan.constants.Constants;
import com.anan.mapper.ChartMapper;
import com.anan.mapper.ImgResourceMapper;
import com.anan.mapper.StudentMapper;
import com.anan.pojo.bo.ChartBO;
import com.anan.pojo.domain.ChartDO;
import com.anan.pojo.domain.ImgResourceDO;
import com.anan.pojo.domain.ProductDO;
import com.anan.pojo.domain.StudentDO;
import com.anan.pojo.dto.ChartDTO;
import com.anan.pojo.dto.ChartInfoDTO;
import com.anan.service.ChartService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author anan
 * @date 2023/05/12/03:00
 **/
@Service
public class ChartServiceImpl implements ChartService {

    @Resource
    private ChartMapper chartMapper;
    @Resource
    private ImgResourceMapper imgResourceMapper;
    @Resource
    private StudentMapper studentMapper;

    @Override
    public List<ChartBO> getCharts(ChartDTO chartDTO) {
        if (null != chartDTO) {
            List<ChartBO> charts = new ArrayList<>();

            //查询商品的聊天内容
            List<ChartDO> chartDOS = chartMapper.getChartsByTypeAndId(chartDTO.getStudentId());

            chartDOS.forEach(chartDO -> {
                ChartBO chartBO = new ChartBO();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
                chartBO.setCreateDate(dateFormat.format(chartDO.getCreateDate()));
                BeanUtils.copyProperties(chartDO, chartBO);
                ImgResourceDO imgResourceDO = imgResourceMapper.getHeadUrlByStudentId(Constants.TYPE.STUDENT, chartDO.getStudentId());
                if (null != imgResourceDO) chartBO.setHeadUrl(imgResourceDO.getImgUrl());
                StudentDO studentDO = studentMapper.getStudentById(chartDO.getStudentId());
                chartBO.setNickName(studentDO.getNickname());
                charts.add(chartBO);
            });
            return charts;
        }
        return null;
    }

    @Override
    public boolean sentChart(ChartInfoDTO chartInfoDTO) {
        if (null != chartInfoDTO) {
            boolean flag = false;
            //添加商品的聊天
            return chartMapper.insertChart(chartInfoDTO.getStudentId(),  chartInfoDTO.getChart());
        }
        return false;
    }
}
